﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using IntelligentEscalator.BUS;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class LogController : ControllerBase
    {
        public IHttpContextAccessor _contextAccessor;
        private JwtSettings _jwtSettings;
        public LogController(IHttpContextAccessor contextAccessor,IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _contextAccessor = contextAccessor;
            _jwtSettings = jwtSettingsAccesser.Value;
        }

        /// <summary>
        /// 查询错误日志
        /// </summary>
        /// <param name="errorLogInfo">错误日志信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryErrorLog")]
        public ResponseMessage QueryErrorLog(ErrorLogInfo errorLogInfo)
        {
            ResponseMessage message = new ResponseMessage();
            LogBUS BUS = new LogBUS();
            try
            {
                message = BUS.QueryErrorLog(errorLogInfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询错误日志失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
            }
            return message;
        }

        /// <summary>
        /// 查询操作日志
        /// </summary>
        /// <param name="operationLog">操作日志信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryOperationLog")]
        public ResponseMessage QueryOperationLog(OperationLog operationLog)
        {
            ResponseMessage message = new ResponseMessage();
            LogBUS BUS = new LogBUS();
            try
            {
                message = BUS.QueryOperationLog(operationLog);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询操作日志失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
            }
            return message;
        }

        /// <summary>
        /// 查询操作日志
        /// </summary>
        /// <param name="clearLog">清除日志信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryClearLog")]
        public ResponseMessage QueryClearLog(ClearLog clearLog)
        {
            ResponseMessage message = new ResponseMessage();
            LogBUS BUS = new LogBUS();
            try
            {
                message = BUS.QueryClearLog(clearLog);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询清除日志失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
            }
            return message;
        }

        /// <summary>
        /// 查询登录登出日志
        /// </summary>
        /// <param name="loginLog">登录登出日志信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryLoginLog")]
        public ResponseMessage QueryLoginLog(LoginLog loginLog)
        {
            ResponseMessage message = new ResponseMessage();
            LogBUS BUS = new LogBUS();
            try
            {
                message = BUS.QueryLoginLog(loginLog);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询登录登出日志失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
            }
            return message;
        }

        /// <summary>
        /// 查询远程锁梯日志
        /// </summary>
        /// <param name="lockLog">远程锁梯日志信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryLockLog")]
        public ResponseMessage QueryLockLog(LockLog lockLog)
        {
            ResponseMessage message = new ResponseMessage();
            LogBUS BUS = new LogBUS();
            try
            {
                message = BUS.QueryLockLog(lockLog);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询远程锁梯日志失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
            }
            return message;
        }
    }
}
